/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package lcfgly.spring;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;

/**
 * LFileUtils
 * 文件操作类，基于spring包
 * @author lcfgly
 * @Date 2015-1-29 9:26:51
 * @version 1.0
 */
public class LFileUtils {

     /**
     * 获取资源文件输入流
     * @param filepath
     * @return
     * @throws IOException 
     */
    public static InputStream GetFileInputStream(String filepath) throws IOException{
        DefaultResourceLoader resourceloader=new DefaultResourceLoader();
        Resource resource=  resourceloader.getResource(filepath);
        return resource.getInputStream();
    }
    
    /**
     * 获取一个解析好的properties配置文件数据
     * @param filepath
     * @return
     * @throws IOException 
     */
    public static Properties GetProperties(String filepath) throws IOException{
        Properties properties=new Properties();
        InputStream in=GetFileInputStream(filepath);
        properties.load(in);
        in.close();
        return properties;
    }
  
    /**
     * 修改文件名
     * @param oldFile 原始文件
     * @param newname 新名称
     * @return 
     */
    public static boolean Rename(File oldFile,String newname){
        if(!oldFile.exists())
        {
            try {
                oldFile.createNewFile();
            } catch (IOException ex) {
                System.err.println(ex.getMessage());
            }
        }
        String rootPath = oldFile.getParent();
        File newFile = new File(rootPath + File.separator + newname);
        return oldFile.renameTo(newFile);
    }
    
}
